# template elements
# presentation

R Workshop

Module 6: “To Infinity and Beyond” (1)

2018-04-18
Bobae Kang
(Bobae.Kang@illinois.gov)

Agenda

  • Part 1: Sharing your work
  • Part 2: Leveraging online resources

R Markdown

plot of chunk unnamed-chunk-1

Source: R Markdown

What is R Markdown?

“R Markdown is a file format for making dynamic documents with R. An R Markdown document is written in markdown (an easy-to-write plain text format) and contains chunks of embedded R code.”

- Garret Grolemund from R Studio

Why use R Markdown?

  • Reproducible
  • Multiple document formats
  • Embedding R code chunks (and the outputs)

Getting started

  • Install rmarkdown
    • not necessary if using R Studio IDE
  • Create a R markdown document
    • File > New File > R Markdown
  • “Knit” the R markdown document into a file in the desired format

plot of chunk unnamed-chunk-2

plot of chunk unnamed-chunk-3

plot of chunk unnamed-chunk-4

plot of chunk unnamed-chunk-5

Basics

  • YAML header
  • knit and preview outputs
  • markdown
  • code chucks

yaml header plot of chunk unnamed-chunk-6

YAML header options

  • Title, author, date
  • output
    • output format
    • output options
    • different output format has different options

knit and prevew plot of chunk unnamed-chunk-7

knit options plot of chunk unnamed-chunk-8

preview options plot of chunk unnamed-chunk-9

markdown plot of chunk unnamed-chunk-10

Markdown basics

  • Headers
  • Font types (italic, bold, strikethrough, superscript)
  • Lists (ordered, unordered)
  • Hyperlinks
  • Images
  • Blackquotes
  • Horizontal line/page break
  • Math equations (using LaTeX)
  • Code/R code (chunk, in-line)

Headers

# Header 1

## Header 2

### Header 3

Font types

_italic_  __bold__

*italic*  **bold**


~~strikethrough~~


superscript^2^

Lists

Unordered list  |  Ordered list
                | 
* Item 1        |  1. Item 1
    * Item 1a   |      1. Item 1a
* Item 2        |  2. Item 2
    + Item 2a   |      1. Item 2a
    - Item 2b   |      2. Item 2b
Mixed list

1. Item 1
    * Item 1a
* Item 2
    1. Item 2a
    + Item 2b

Hyperlinks

[text with hyperlink](http://link.path)

Images

![](http://image.path/example.png)

![optional caption text](image/example.png)

Blockquotes

> A line of text following the "> " is a blockquote.

A line of text following the “> ” is a blockquote.

Horizontal line/page break

More then three asteriks or dashs

***

******

------

Math equations

Inline math equations look like: $y = (x + 1)^2$
Inline math equations look like: \( y = (x + 1)^2 \)


A block of math equations look like:

$$y = x^2 + 2x + 1$$
A block of math equations look like: \[ y = x^2 + 2x + 1 \]

R code chunk plot of chunk unnamed-chunk-11

Insert a new code chunk plot of chunk unnamed-chunk-12

  • keyboard shortcut: Ctrl + Alt + i

Run code chunks plot of chunk unnamed-chunk-13

  • keyboard shortcut: Ctrl + Enter

knitr options

  • Global options
    • Act as the default settings for the whole document
  • Local options (per code chunk)
    • Only applied to the specific code chunck
    • Overide the global options if relevant

Images

  • Insert images with knitr::include_graphics()
  • Image size
  • Image alignment

Using knitr::include_graphics()

# image on the web using URL
knitr::include_graphics("https://www.some-image.com/image1.jpg")

# image in the project folder using a relative path
knitr::include_graphics("images/image2.png")

knitr options for image

  • fig.height and fig.width
  • fig.dim

Tables

  • Printing basic data frame object
  • Better looking tables with knitr::kable()
  • Interactive tables with DT::datatable()

default data frame example

kable example

datatable example

Documents

Creating R Markdown documents

HTML documents

---
title: "My first R Markdown document"
output: html_document
---

R Notebooks

---
title: "My first R Notebook"
output: html_notebook
---

“An R Notebook is an R Markdown document with chunks that can be executed independently and interactively, with output visible immediately beneath the input.”
- “R Notebooks”, RStudio

htmlwidgets for R

  • Thera are many R packages (90+) for taking full advantage of the interactivity that web can offer.
  • With these packages, we can easily incorporate interactive widgets into HTML documents generated using R Markdown
  • Examples of htmlwidgets include:
    • plotly and highcharter for interactive visualizations
    • leaflet for interactive maps
    • DT for interactive data tables
  • Visit htmlwidgets for R website to find out more

PDF documents

---
title: "My PDF document"
output: pdf_document
---

tufte and tint

Word documents

---
title: "My word document"
output: word_document
---

Other formats

  • d
  • d
  • d

RPubs

“RPubs is a quick and easy way to disseminate data analysis and R code and do ad-hoc collaboration with peers.”
- RStudio Team

  • Publishing an R Markdown output on RPubs is as easy as clicking the “publish” button on RStudio
  • Comparable to Tableau Public's Gallery website(?)

Presentations

Creating presentation slides

  • RStudio supports many ways to create presentation slides that are both modern-looking and highly customizable
  • Popular ways to create slides using R include:
    • R Markdown formats:
      • ioslides_presentation (HTML)
      • revealjs::revealjs_presentation (HTML)
      • slidy_presentation (HTML)
      • beamer_presentation (PDF)
    • R Presentation (HTML)

ioslides

---
title: "My first ioslide presentation"
output: ioslides_presentation
---
  • ioslides output format is built into RStudio
  • Fully integrates R Markdown syntax
    • Creating new slides is as easy as using # and ## headings
  • To learn more about ioslides, read the “Presentations with ioslides” article on RStudio

Slides example

---
title: "My first ioslide presentation"
author: Bobae Kang
date: April 18, 2018
output: ioslides_presentation
---

# First section


## Normal slide

- Item one
- Item two

## Another slide | With a subtitle

This slide has a two-column layout

----

![picture saying hello](images/hello.png)

Embed sample ioslides here

revealjs

---
title: "My first revealjs presentation"
output: revealjs::revealjs_presentation
---
  • With revealjs R package, it is possible to use R Markdown (and its syntax) to generate slides using reveal.js, a JavaScript library for interactive slides in HTML
  • To learn more about revealjs, read the “Presentations with reveal.js” article on RStudio

Embed sample revealjs here

Other formats

  • HTML presentations with slidy
    • Use output: slidy_presentation in the YAML header
    • See the “Presentations with Slidy” article on RStudio
  • PDF presentations with beamer
    • Use output: beamer_presentation
    • See the “Presentations with Beamer” article on RStudio
  • HTML presentations with xaringan

R Presentation

  • Not a R Markdown document
  • All ICJIA R Workshop slides are generated using R Presentation
  • To learn more R Presentation, visit relevant pages on RStudio Support website

Shiny

plot of chunk unnamed-chunk-18

Source: R Studio

What is Shiny?

“Shiny is an open source R package that provides an elegant and powerful web framework for building web applications using R. Shiny helps you turn your analyses into interactive web applications without requiring HTML, CSS, or JavaScript knowledge.”
- RStudio.com

Dashboards

  • A data dashboard is a visual interface to data to allow its viewers for gain key insights
  • It is often an interactive application that provides viewers with options to explore data from multiple angles interactively
  • Shiny application can make a great dashboard

Interactive documents

  • Shiny can be used to generate interactive documents with Shiny app widgets
  • An interactive document is an R Markdown file with the YAML head including runtime: shiny and output: html_document or output: ioslids_presentation

Getting started

Shiny application

Components

Deployment

Deploying via shinyapps.io

Deploying via ShinyProxy

shinydashboard

shinyjs

Websites

Static websites

  • A collection R Markdown documents in HTML format can be made into a website
    • To do so, we need a _site.yml file in the same folder as individual documents to be put together
    • _site.yml specifies the name and the routing structure of the resulting website
    • With _site.yml and all HTML documents ready, run rstudio::render_site() to generate the website
  • For details, see the “R Markdown Websites” article on RStudio website

Hosting websites on Github Pages

“GitHub Pages is a static site hosting service designed to host your personal, organization, or project pages directly from a GitHub repository.”
-“What is GitHub Pages?”, GitHub Help

  • GitHub is a free and commercial online repository service for storing and sharing projects using Git
  • The ICJIA R Workshop website is hosted on GitHub Pages
  • Read more about GitHub Pages and hosting static sites on GitHub Pages on GitHub Help here

Books with blogdown

  • bookdown package, built on R Markdown, facilitates writing books and long articles/reports.
    • A bookdown publication is made downloadable in PDF, EPUB and MOBI formats
  • Check out bookdown package website to find out more
  • Also, read Xie, Y. (2018). bookdown: Authoring Books and Technical Documents with R Markdown for a comprehensive guide for bookdown
    • The book itself is generated using bookdown

Blogs with blogdown

  • blogdown is a package to generate static websites using R Markdown and the Hugo open-source framework for building websites
  • Visit Awesome Blogdown website for a curated list of blogdown examples
  • Also, read Xie, Y. et al. (2018). blogdown: Creating Websites with R Markdown for a comprehensive guide for blogdown

Questions?

plot of chunk unnamed-chunk-24

Source: Giphy

References

plot of chunk unnamed-chunk-25

Source: Wikimedia.org